﻿class tower{
	private var towerBlocks:Array;
	private var maxHeight = 6;
	private var owner;
	private var parent:player;
	
	function tower(mHeight:Number, o:String){
		parent = null;
		maxHeight = mHeight;
		owner = o;
		towerBlocks = Array();
	}
	
	/**
	 * Returns the owner of this tower
	 * @param the owner of this tower (eg, left or right)	 */
	public function getOwner():String{
		return owner;
	}
	
	/**
	 * Returns the x-coordinate of the base of this tower
	 * @return the x-coordinate of the base of this tower	 */
	public function getXPos():Number{
		//returns the x-position of the tower
		return towerBlocks[0]._x;
	}
	
	/**
	 * Returns the lowest towerBlock in the tower
	 * @param the base towerBlock of the tower	 */
	public function getBase():towerBlock{
		//returns the base piece of the tower
		return towerBlocks[0];
	}
	
	/**
	 * Sets the parent to the specified value
	 * @param p the new value for parent	 */
	public function setParent(p:player):Void{
		parent = p;	
	}
	
	/**
	 * Gets the parent for this tower
	 * @return the parent of this tower (a player)	 */
	public function getParent():player{
		return parent;	
	}
	
	/**
	 * Returns the highest towerBlock in the tower
	 * @param the top towerBlock of the tower	 */
	public function getTop():towerBlock{
		//returns the top piece of the tower
		return towerBlocks[towerBlocks.length - 1];
	}
	
	/**
	 * Adds the specified piece to the tower
	 * @param piece the piece to be added to the tower
	 * @return true if the piece could be added and false otherwise	 */
	public function addPiece(piece:MovieClip):Boolean{
		//returns true if the piece could be added and false otherwise
		piece.parent = this;
		
		if(this.towerBlocks.length < maxHeight){
			this.towerBlocks.push(piece);
			return true;
		}
		else
			return false;
	}
	
	/**
	 * Removes the specified piece from the tower, shifting pieces downward if necessary
	 * @param piece the piece to be removed
	 * @param wizard the wizard to shift downward (if necessary)	 */
	public function removePiece(piece:towerBlock, wizard:unit):Void{
		piece.setParent(this);
	
		var dropPiece:Boolean = false;			//if this is true, the piece will be dropped (shifted down)
		
		for(var i = 0; i < towerBlocks.length; i++){
			if(dropPiece == true){
				towerBlocks[i]._y += piece._height;
			}
			
			if(towerBlocks[i] == piece){
				wizard._y += piece._height
				dropPiece = true;
			}
				
		}
		
		towerBlocks.remove(piece);
	}
	
	/**
	 * Adds the piece right after the top piece
	 * @param piece the piece to be added to this tower
	 * @param wizard the wizard to shift upwards if necessary
	 * @return true if the piece could be added and false otherwise	 */
	function addPieceAfterTop(piece:towerBlock, wizard:unit):Boolean{
		//returns true if the piece could be added and false otherwise
		//inserts the piece after the top piece
		piece.setParent(this);

		if(this.towerBlocks.length < maxHeight){
			if(this.towerBlocks.length == 0)
				trace("ERROR: towerBlocks.length = 0; tower.as");
			var tmp = this.towerBlocks.pop();
			this.towerBlocks.push(piece);

			//if there is another piece, get its position and we can determine the
			//position of the new piece, based on that
			if(this.towerBlocks.length > 0){
				piece._x = this.towerBlocks[0]._x;
				//get the previous piece, and add the new piece right above that
				piece._y = this.towerBlocks[(towerBlocks.length - 2)]._y - piece._height;
			}
			else{
				//this is the new base of the tower
			}
			
			tmp._y -= piece._height;
			wizard._y -= wizard._height;

			this.towerBlocks.push(tmp);

			return true;
		}
		else{
			piece.removeMovieClip();
			return false;
		}
		
		
	}	
}